package com.zl.learning.thread;

import java.util.concurrent.*;

/**
 * Created by zhaolei on 2017/7/2.
 */
public class ThreadPoolExecutorTest {

    /**
     * 创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程
     */
    public static class TestCachedThreadPool {

        public static void main(String[] args) throws ExecutionException, InterruptedException {
            ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
            for (int i = 0; i < 10; i++) {
                final int index = i;
                try {
                    Thread.sleep(index * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                cachedThreadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(index);
                    }
                });
            }

            Future<Integer> res = cachedThreadPool.submit(() -> {
                TimeUnit.SECONDS.sleep(3);
                return 1;
            });
            System.out.println(res.get());
        }

    }

    /**
     * 创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待
     */
    public static class TestFixedThreadPool {

        public static void main(String[] args) {
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
            for (int i = 0; i < 100; i++) {
                final int index = i;
                fixedThreadPool.execute(new Runnable() {
                    public void run() {
                        try {
                            System.out.println(index);
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }

    }

    /**
     * 创建一个定长线程池，支持定时及周期性任务执行
     */
    public static class TestScheduledThreadPool {

        public static void main(String[] args) {
            ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);

            //表示延迟3秒执行
            scheduledThreadPool.schedule(new Runnable() {
                public void run() {
                    System.out.println("delay 3 seconds");
                }
            }, 3, TimeUnit.SECONDS);

            //表示延迟1秒后每3秒执行一次
            scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
                public void run() {
                    System.out.println("delay 1 seconds, and excute every 3 seconds");
                }
            }, 1, 3, TimeUnit.SECONDS);

            scheduledThreadPool.shutdown();
        }

    }

    /**
     * 创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务，保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行
     */
    public static class TestSingleThreadExecutor {

        public static void main(String[] args) {
            ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

//        结果依次输出，相当于顺序执行各个任务
            for (int i = 0; i < 10; i++) {
                final int index = i;
                singleThreadExecutor.execute(() -> {
                    Thread.currentThread().setName("线程"+index);
                    try {
                        System.out.println(Thread.currentThread().getName()+"---"+index);
                        TimeUnit.SECONDS.sleep(7);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
            singleThreadExecutor.shutdown();
        }
    }

}
